#ifndef _BSP_USART_H
#define _BSP_USART_H

#include "main.h"

#include "gd32f30x.h"
#include <stdio.h>

#define MODBUS_IAP_ADR           ((u8)0x5A)  //帧头
#define UART0_BUF_LENTH 256
#define UART_BUFFER_SIZE 256

enum rs485_mode {
	RX = 0,
	TX = 1,
};

enum uart_bus_state {
    FRAME_END = 0,  //有5~10ms没收到数据，认为一帧结束
    WAITING = 1,  //有0~5ms没收到数据
    RECEIVING = 2,  //正在接收数据
    FREE = 3,  //一帧结束后，没有收到数据
};

// struct uart_dev {
// 	uint32_t reg_addr;  //寄存起始地址
// 	uint8_t frame_end_flag;
// 	uint8_t rx_buf[UART_BUFFER_SIZE];
// 	uint16_t rx_cnt;
// 	void (*init)(uint32_t bps);
// 	void (*tx)(uint8_t* buf, uint16_t len);
// };
// extern struct uart_dev uart0;
// extern struct uart_dev uart2;

/****************/
#define printk(fmt, args...)	do{\
								rs485_mode_set(TX);\
								printf(fmt, ##args);\
								while (usart_flag_get(USART0, USART_FLAG_TC) == RESET);\
								rs485_mode_set(RX);\
								} while (0)


extern uint16_t uart0_rx_cnt;
extern uint8_t uart0_frame_end_flag;  //帧结束标志
extern uint8_t uart0_rx_buf[UART0_BUF_LENTH];

void bsp_usart0_init(uint32_t bps);
void rs485_delay(void);
void rs485_mode_set(enum rs485_mode);
void uart0_tx(uint8_t *str, uint16_t strlen);

#endif
